package cn.minimal;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class MyJPanel extends JPanel implements KeyListener, Runnable {
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 800, 600);
        this.drawTank(g, TankGameJFrame.lYTank1);
        for(int i = 0; i < TankGameJFrame.E_NUM; i++) {
            this.drawETank(g, TankGameJFrame.eTanks[i]);
        }
        g.setColor(Color.WHITE);
        g.drawString("我方坦克阵亡次数：" + TankGameJFrame.dead_times + "，消灭敌方坦克数量：" + TankGameJFrame.enemy_dead_times, 10, 530);
    }
    public void drawTank(Graphics g, Tank t) {
        switch (t.type) {
            case 0:
                g.setColor(Color.RED);
                break;
            case 1:
                g.setColor(Color.BLUE);
                break;
        }
        switch (t.direction) {
            case 0:
                if(t.isLive) {
                    g.fill3DRect(t.x, t.y, 5, 30, false);
                    g.fill3DRect(t.x + 5, t.y + 5, 10, 20, false);
                    g.fill3DRect(t.x + 15, t.y, 5, 30, false);
                    g.drawOval(t.x + 8, t.y + 11, 4, 4);
                    g.drawLine(t.x + 10, t.y, t.x + 10, t.y + 15);
                    if(t.shot != null && t.shot.isLive) {
                        g.fill3DRect(t.shot.getX()-2, t.shot.getY(), 5, 5, false);
                        //System.out.println(shot.getX() + "," + shot.getY());
                    }
                }
                if(t.bomb != null && t.bomb.isLive) {
                    g.drawImage(t.bomb.im1, t.bomb.x, t.bomb.y, 20, 20, this);
                    g.drawImage(t.bomb.im2, t.bomb.x, t.bomb.y, 20, 20, this);
                    g.drawImage(t.bomb.im3, t.bomb.x, t.bomb.y, 20, 20, this);
                    t.bomb_time--;
                    if(t.bomb_time == 0) {
                        t.bomb.setLive(false);
                        t.bomb_time = 3;
                    }
                }
                break;
            case 1:
                if(t.isLive) {
                    g.fill3DRect(t.x, t.y, 30, 5, false);
                    g.fill3DRect(t.x + 5, t.y + 5, 20, 10, false);
                    g.fill3DRect(t.x, t.y + 15, 30, 5, false);
                    g.drawOval(t.x + 11, t.y + 8, 4, 4);
                    g.drawLine(t.x + 15, t.y + 10, t.x + 30, t.y + 10);
                    if(t.shot != null && t.shot.isLive) {
                        g.fill3DRect(t.shot.getX(), t.shot.getY()-2, 5, 5, false);
                        //System.out.println(shot.getX() + "," + shot.getY());
                    }
                }
                if(t.bomb != null && t.bomb.isLive) {
                    g.drawImage(t.bomb.im1, t.bomb.x, t.bomb.y, 20, 20, this);
                    g.drawImage(t.bomb.im2, t.bomb.x, t.bomb.y, 20, 20, this);
                    g.drawImage(t.bomb.im3, t.bomb.x, t.bomb.y, 20, 20, this);
                    t.bomb_time--;
                    if(t.bomb_time == 0) {
                        t.bomb.setLive(false);
                        t.bomb_time = 3;
                    }
                }
                break;
            case 2:
                if(t.isLive) {
                    g.fill3DRect(t.x, t.y, 5, 30, false);
                    g.fill3DRect(t.x + 5, t.y + 5, 10, 20, false);
                    g.fill3DRect(t.x + 15, t.y, 5, 30, false);
                    g.drawOval(t.x + 8, t.y + 11, 4, 4);
                    g.drawLine(t.x + 10, t.y + 15, t.x + 10, t.y + 30);
                    if(t.shot != null && t.shot.isLive) {
                        g.fill3DRect(t.shot.getX()-2, t.shot.getY(), 5, 5, false);
                        //System.out.println(shot.getX() + "," + shot.getY());
                    }
                }
                if(t.bomb != null && t.bomb.isLive) {
                    g.drawImage(t.bomb.im1, t.bomb.x, t.bomb.y, 20, 20, this);
                    g.drawImage(t.bomb.im2, t.bomb.x, t.bomb.y, 20, 20, this);
                    g.drawImage(t.bomb.im3, t.bomb.x, t.bomb.y, 20, 20, this);
                    t.bomb_time--;
                    if(t.bomb_time == 0) {
                        t.bomb.setLive(false);
                        t.bomb_time = 3;
                    }
                }
                break;
            case 3:
                if(t.isLive) {
                    g.fill3DRect(t.x, t.y, 30, 5, false);
                    g.fill3DRect(t.x + 5, t.y + 5, 20, 10, false);
                    g.fill3DRect(t.x, t.y + 15, 30, 5, false);
                    g.drawOval(t.x + 11, t.y + 8, 4, 4);
                    g.drawLine(t.x, t.y + 10, t.x + 15, t.y + 10);
                    if(t.shot != null && t.shot.isLive) {
                        g.fill3DRect(t.shot.getX(), t.shot.getY()-2, 5, 5, false);
                        //System.out.println(shot.getX() + "," + shot.getY());
                    }
                }
                if(t.bomb != null && t.bomb.isLive) {
                    g.drawImage(t.bomb.im1, t.bomb.x, t.bomb.y, 20, 20, this);
                    g.drawImage(t.bomb.im2, t.bomb.x, t.bomb.y, 20, 20, this);
                    g.drawImage(t.bomb.im3, t.bomb.x, t.bomb.y, 20, 20, this);
                    t.bomb_time--;
                    if(t.bomb_time == 0) {
                        t.bomb.setLive(false);
                        t.bomb_time = 3;
                    }
                }
                break;
        }
    }
    public void drawETank(Graphics g, eTank t) {
        switch (t.type) {
            case 0:
                g.setColor(Color.RED);
                break;
            case 1:
                g.setColor(Color.BLUE);
                break;
        }
        switch (t.direction) {
            case 0:
                if(t.isLive) {
                    g.fill3DRect(t.x, t.y, 5, 30, false);
                    g.fill3DRect(t.x + 5, t.y + 5, 10, 20, false);
                    g.fill3DRect(t.x + 15, t.y, 5, 30, false);
                    g.drawOval(t.x + 8, t.y + 11, 4, 4);
                    g.drawLine(t.x + 10, t.y, t.x + 10, t.y + 15);
                    if(t.shot != null && t.shot.isLive) {
                        g.fill3DRect(t.shot.getX()-2, t.shot.getY(), 5, 5, false);
                        //System.out.println(shot.getX() + "," + shot.getY());
                    }
                }
                if(t.bomb != null && t.bomb.isLive) {
                    g.drawImage(t.bomb.im1, t.bomb.x, t.bomb.y, 20, 20, this);
                    g.drawImage(t.bomb.im2, t.bomb.x, t.bomb.y, 20, 20, this);
                    g.drawImage(t.bomb.im3, t.bomb.x, t.bomb.y, 20, 20, this);
                    t.bomb_time--;
                    if(t.bomb_time == 0) {
                        t.bomb.setLive(false);
                        t.bomb_time = 3;
                    }
                }
                break;
            case 1:
                if(t.isLive) {
                    g.fill3DRect(t.x, t.y, 30, 5, false);
                    g.fill3DRect(t.x + 5, t.y + 5, 20, 10, false);
                    g.fill3DRect(t.x, t.y + 15, 30, 5, false);
                    g.drawOval(t.x + 11, t.y + 8, 4, 4);
                    g.drawLine(t.x + 15, t.y + 10, t.x + 30, t.y + 10);
                    if(t.shot != null && t.shot.isLive) {
                        g.fill3DRect(t.shot.getX(), t.shot.getY()-2, 5, 5, false);
                        //System.out.println(shot.getX() + "," + shot.getY());
                    }
                }
                if(t.bomb != null && t.bomb.isLive) {
                    //System.out.println("here1");
                    g.drawImage(t.bomb.im1, t.bomb.x, t.bomb.y, 20, 20, this);
                    g.drawImage(t.bomb.im2, t.bomb.x, t.bomb.y, 20, 20, this);
                    g.drawImage(t.bomb.im3, t.bomb.x, t.bomb.y, 20, 20, this);
                    t.bomb_time--;
                    if(t.bomb_time == 0) {
                        t.bomb.setLive(false);
                        t.bomb_time = 3;
                    }
                }
                break;
            case 2:
                if(t.isLive) {
                    g.fill3DRect(t.x, t.y, 5, 30, false);
                    g.fill3DRect(t.x + 5, t.y + 5, 10, 20, false);
                    g.fill3DRect(t.x + 15, t.y, 5, 30, false);
                    g.drawOval(t.x + 8, t.y + 11, 4, 4);
                    g.drawLine(t.x + 10, t.y + 15, t.x + 10, t.y + 30);
                    if(t.shot != null && t.shot.isLive) {
                        g.fill3DRect(t.shot.getX()-2, t.shot.getY(), 5, 5, false);
                        //System.out.println(shot.getX() + "," + shot.getY());
                    }
                }
                if(t.bomb != null && t.bomb.isLive) {
                    //System.out.println("here2");
                    g.drawImage(t.bomb.im1, t.bomb.x, t.bomb.y, 20, 20, this);
                    g.drawImage(t.bomb.im2, t.bomb.x, t.bomb.y, 20, 20, this);
                    g.drawImage(t.bomb.im3, t.bomb.x, t.bomb.y, 20, 20, this);
                    t.bomb_time--;
                    if(t.bomb_time == 0) {
                        t.bomb.setLive(false);
                        t.bomb_time = 3;
                    }
                }
                break;
            case 3:
                if(t.isLive) {
                    g.fill3DRect(t.x, t.y, 30, 5, false);
                    g.fill3DRect(t.x + 5, t.y + 5, 20, 10, false);
                    g.fill3DRect(t.x, t.y + 15, 30, 5, false);
                    g.drawOval(t.x + 11, t.y + 8, 4, 4);
                    g.drawLine(t.x, t.y + 10, t.x + 15, t.y + 10);
                    if(t.shot != null && t.shot.isLive) {
                        g.fill3DRect(t.shot.getX(), t.shot.getY()-2, 5, 5, false);
                        //System.out.println(shot.getX() + "," + shot.getY());
                    }
                }
                if(t.bomb != null && t.bomb.isLive) {
                    //System.out.println("here3");
                    g.drawImage(t.bomb.im1, t.bomb.x, t.bomb.y, 20, 20, this);
                    g.drawImage(t.bomb.im2, t.bomb.x, t.bomb.y, 20, 20, this);
                    g.drawImage(t.bomb.im3, t.bomb.x, t.bomb.y, 20, 20, this);
                    t.bomb_time--;
                    if(t.bomb_time == 0) {
                        t.bomb.setLive(false);
                        t.bomb_time = 3;
                    }
                }
                break;
        }
    }
    @Override
    public void keyTyped(KeyEvent keyEvent) {
    }
    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_UP) {
            TankGameJFrame.lYTank1.moveUp();
        } else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
            TankGameJFrame.lYTank1.moveRight();
        } else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
            TankGameJFrame.lYTank1.moveDown();
        } else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
            TankGameJFrame.lYTank1.moveLeft();
        } else if(e.getKeyCode() == KeyEvent.VK_SPACE) {
            TankGameJFrame.lYTank1.showShot();
        }
        this.repaint();
    }
    @Override
    public void keyReleased(KeyEvent keyEvent) {
    }
    @Override
    public void run() {
        while(true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
            checkBomb();
            reinitializeTanks();
            this.repaint();
        }
    }
    public void reinitializeTanks() {
        if(!TankGameJFrame.lYTank1.isLive) {
            TankGameJFrame.lYTank1.revive_time--;
            if (TankGameJFrame.lYTank1.revive_time == 0) {
                TankGameJFrame.lYTank1 = new Tank(10, 10, 0, 0);
            }
        }
        for(int i = 0; i < TankGameJFrame.E_NUM; i++) {
            if(!TankGameJFrame.eTanks[i].isLive) {
                TankGameJFrame.eTanks[i].revive_time--;
                if(TankGameJFrame.eTanks[i].revive_time == 0) {
                    TankGameJFrame.eTanks[i] = new eTank(500, 10, 1, 3);
                    Thread t = new Thread(TankGameJFrame.eTanks[i]);
                    t.start();
                }
            }
        }
    }
    public void checkBomb() {
        for(int i = 0; i < TankGameJFrame.E_NUM; i++) {
            eTank e = TankGameJFrame.eTanks[i];
            // 首先检查我方坦克是否被子弹击中
            if(e.shot != null && e.shot.isLive) {
                if( e.shot.x >= TankGameJFrame.lYTank1.x && e.shot.x <= TankGameJFrame.lYTank1.x + 20 && e.shot.y >= TankGameJFrame.lYTank1.y && e.shot.y <= TankGameJFrame.lYTank1.y + 20) {
                    Bomb bomb = new Bomb(TankGameJFrame.lYTank1.x, TankGameJFrame.lYTank1.y);
                    bomb.setLive(true);
                    TankGameJFrame.lYTank1.bomb = bomb;
                    TankGameJFrame.lYTank1.isLive = false;
                    TankGameJFrame.eTanks[i].shot.isLive = false;
                    TankGameJFrame.dead_times += 1;
                }
            }
            // 检查敌方坦克是否被子弹击中
            if(TankGameJFrame.lYTank1.shot != null && TankGameJFrame.lYTank1.shot.isLive) {
                if( TankGameJFrame.lYTank1.shot.x >= e.x && TankGameJFrame.lYTank1.shot.x <= e.x + 20 && TankGameJFrame.lYTank1.shot.y >= e.y && TankGameJFrame.lYTank1.shot.y <= e.y + 20) {
                    Bomb bomb = new Bomb(e.x, e.y);
                    bomb.setLive(true);
                    TankGameJFrame.eTanks[i].bomb = bomb;
                    TankGameJFrame.eTanks[i].isLive = false;
                    TankGameJFrame.lYTank1.shot.isLive = false;
                    TankGameJFrame.enemy_dead_times += 1;
                }
            }
        }
    }
}
